# -*- coding: UTF-8 -*-
# __author__ = '10459'

# 处理获取的原始数据，展示对应满足业务需求的展示，不如原始数据裁剪，合并，修饰等
# 思想，就是集合中对应单个，与多个分别封装成不同的类和不同的函数
class BookViewModel:
    def __init__(self, book):
        self.title = book['title']
        self.pages = book['pages'] or ''
        self.image = book['image']
        self.price = book['price']
        self.summary = book['summary']
        self.publisher = book['publisher']
        self.author = "、".join(book['author'])
        self.isbn=book['isbn']
        self.pubdate=book['pubdate']
        self.binding=book['binding']
    @property
    def intro(self):
        intros=filter(lambda x:True if x else False,[self.author,self.publisher,self.price])
        return '/'.join(intros)


class BookCollection:
    def __init__(self):
        self.total = 0
        self.books = []
        self.keyword = ''

    # yushu_book表示书籍原始数据
    def fill(self, yushu_book, keyword):
        self.total = yushu_book.total
        self.keyword=keyword
        self.books=[BookViewModel(book) for book in yushu_book.books]


class _BookViewModel:
    # 单本书处理
    @classmethod
    def package_single(cls, data, keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = 1
            returned['books'] = [cls.__cut_book_data(data)]
        return returned

    # 多本书的处理
    @classmethod
    def package_collection(cls, data, keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = data['total']  # 非len(data['books']),这个只会记录当页的总数，会有问题
            # 字典里面嵌套集合，集合中又嵌套字典，这个常见，里面那个是列表推导式，python基础重要的东西
            returned['books'] = [cls.__cut_book_data(book) for book in data['books']]
        return returned

    @classmethod
    def __cut_book_data(cls, data):
        # or 从左至右都为假打印后面那个
        book = {
            'title': data['title'],
            'pages': data['pages'] or '',
            'image': data['image'],
            'price': data['price'] or '-',
            'summary': data['summary'] or '',
            'publisher': data['publisher'],
            'author': "、".join(data['author'])
        }
        return book
